package com.itheima.service.com.itheima.service.system.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.DeptDao;
import com.itheima.domain.system.Dept;
import com.itheima.factory.MapperFactory;
import com.itheima.service.com.itheima.service.system.DeptService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class DeptServiceImpl implements DeptService {

    @Override
    public void save(Dept dept) {

        SqlSession sqlSession = null;
        try {
            //获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);
            //创建随机的UUID作为ID
            String id = UUID.randomUUID().toString();
            dept.setId(id);
            //调用Dao中的方法保存数据
            deptDao.save(dept);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            //抛出异常
            throw new RuntimeException(e);
        } finally {
            try {
                //关闭SQLSession对象
                sqlSession.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(Dept dept) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao mapper = MapperFactory.getMapper(sqlSession, DeptDao.class);
            int delete = mapper.delete(dept);
            TransactionUtil.commit(sqlSession);
            sqlSession.close();
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            try {
                sqlSession.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void update(Dept dept) {

        //获取SQLSession对象
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            //通过
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);

            deptDao.update(dept);

            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            try {
                sqlSession.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Dept findById(String id) {
        //获取SQLSession对象
        SqlSession sqlSession = null;
        Dept dept = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            //通过
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);

            dept = deptDao.findById(id);

            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            try {
                sqlSession.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return dept;
    }

    @Override
    public List<Dept> findAll() {
        List<Dept> companies = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);
            companies = deptDao.findAll();
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
        return companies;
    }

    @Override
    public PageInfo findAll(int page, int size) {

        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            DeptDao deptDao = MapperFactory.getMapper(sqlSession, DeptDao.class);
            //3.调用Dao层操作
            PageHelper.startPage(page, size);
            List<Dept> all = deptDao.findAll();
            PageInfo pageInfo = new PageInfo(all);
            return pageInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

